<?php
/**
 * Name: 轮播控制器.
 * User: 董坤鸿
 * Date: 2017/11/22
 * Time: 10:06
 */

namespace app\admin\controller;


use app\model\Banners;

class Banner extends Common
{
    /**
     * 定义实例
     *
     * @var
     */
    protected $db;

    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->db = new Banners();
    }

    /**
     * 轮播列表
     *
     * @return \think\response\View
     */
    public function index()
    {
        $input = input('post.');
        $data = $this->db->lists($input, 20);
        $this->assign('data', $data);
        return view();
    }

    /**
     * 新增轮播
     *
     * @return \think\response\View
     */
    public function create()
    {
        return view();
    }

    /**
     * 插入轮播
     */
    public function store()
    {
        $input = input('post.');
        $result = $this->db->store($input);
        if ($result['valid']) {
            //说明执行成功
            $this->success($result['msg'], 'index');
            exit;
        } else {
            //执行失败
            $this->error($result['msg']);
            exit;
        }
    }

    /**
     * 查看轮播
     */
    public function show()
    {
        //TODO:查询轮播
    }

    /**
     * 编辑轮播
     *
     * @return \think\response\View
     */
    public function edit()
    {
        $id = input( 'param.id' );
        $field = $this->db->show($id);
        $this->assign('field', $field);
        return view();
    }

    /**
     * 修改轮播
     */
    public function update()
    {
        $result = $this->db->edit(input('post.'));
        if ($result['valid']) {
            $this->success($result['msg'], 'index');
            exit;
        } else {
            $this->error($result['msg']);
            exit;
        }
    }

    /**
     * 删除轮播
     */
    public function destroy()
    {
        $inputs = input('post.');
        $result = $this->db->del($inputs);
        if ($result['valid']) {
            $this->success($result['msg'], 'index');
            exit;
        } else {
            $this->error($result['msg']);
            exit;
        }
    }

    /**
     * 论文轮播
     */
    public function sort()
    {
        if (request()->isAjax()) {
            $result = $this->db->sort(input('post.'));
            if ($result['valid']) {
                $this->success($result['msg'], 'index');
                exit;
            } else {
                $this->error($result['msg']);
                exit;
            }
        }
    }
}